package org.apache.servicecomb.samples.practise.houserush.realestate.api;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.realestate.entity.Building;
import org.apache.servicecomb.samples.practise.houserush.realestate.entity.House;
import org.apache.servicecomb.samples.practise.houserush.realestate.entity.Realestate;
import org.apache.servicecomb.samples.practise.houserush.realestate.service.HouseService;
import org.apache.servicecomb.samples.practise.houserush.realestate.service.RealestateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.Iterator;
import java.util.List;

@RestSchema(schemaId = "houseApi")
@RequestMapping("/")
public class HouseApiImpl implements HouseApi {
    @Autowired
    private HouseService houseService;

    @Autowired
    private RealestateService realestateService;
    @Override
    @PostMapping("buildings/{buildingId}/houses")
    public House createHouse(@PathVariable("buildingId") Integer buildingId,@RequestBody House house) {
        return houseService.createHouse(buildingId,house);
    }

    @Override
    @GetMapping("houses/{id}")
    public House findHouse(@PathVariable("id") Integer id) {
        return houseService.findHouse(id);
    }

    @Override
    @PutMapping("houses/{id}")
    public House updateHouse(@PathVariable("id") Integer id,@RequestBody House house) {
        house.setId(id);
        return houseService.updateHouse(house);
    }

    @Override
    @DeleteMapping("houses/{id}")
    public void removeHouse(@PathVariable("id") Integer id) {
        houseService.removeHouse(id);
    }

    @Override
    @GetMapping("buildings/{buildingId}/houses")
    public List<House> indexHouses(@PathVariable("buildingId") Integer buildingId) {
        return houseService.indexHouses(buildingId);
    }

    @Override
    @GetMapping("realestates/findTreeRealestate/{id}")
    public Realestate findByRealestateId(@PathVariable("id") Integer realestateId) {
        Realestate realestate = realestateService.findRealestate(realestateId);
        List<Building> buildings = realestate.getBuildings();
        for (Building building : buildings) {
            List<House> houses = building.getHouses();
            Iterator<House> iterator = houses.iterator();
            while(iterator.hasNext()) {
                House house = iterator.next();
                if("locking".equals(house.getState())) {
                    iterator.remove();
                }
            }
        }
        return realestate;
    }
    @Override
    @PutMapping("houses/lock_houses_for_sale")
    public List<House> lockHousesForSale(@RequestBody List<Integer> ids) {
        return houseService.lockHousesForSale(ids);

    }

    @Override
    @PutMapping("houses/releaseLockingStatesForHouses")
    public int updateReleaseLockingStatesForHouses(@RequestBody List<Integer> ids) {
        return houseService.updateReleaseLockingStatesForHouses(ids);
    }
}
